package chp11.homework;
public class MaxNumber {
    public static void main(String[] args) {
        int[] numbers = {3, 8, 2, 9, 5, 1};
        // 使用try-catch来捕获异常
        try {
            findMaxValues(numbers);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        // 测试数组为空的情况
        int[] emptyNumbers = {};
        try {
            findMaxValues(emptyNumbers);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
    public static void findMaxValues(int[] array) throws Exception {
        if (array.length == 0) {
            // 直接使用throw来抛出异常
            throw new Exception("数组为空，无法找到最大值。");
        }
        int max = array[0];
        for (int i = 1; i < array.length; i++) {
            if (array[i] > max) {
                max = array[i];
            }
        }
        System.out.println("max:" + max);
    }
}